<?php

namespace App\Http\Resources;

use App\Models\Car;
use Illuminate\Http\Resources\Json\ResourceCollection;
use App\Helpers\BaseData;

class CarCollection extends ResourceCollection
{
    use BaseData;
    public function toArray($request)
    {
        $collection = $this->collection;

        if($collection->count()<19){
            $other = Car::query()
                ->where('status',1)
                ->orWhereNotNull('non_lease_time')->get();
            if($other){
                foreach ($other as $item){
                    $collection->push($item);
                }
            }
        }

        if($collection->isEmpty()){
            return parent::toArray($request);
        }else{
            $collection->map(function($name)use($request){
                $request->offsetSet('car_id',$name['id']);
                $name['days_price'] = $this->totalPrice($request);
                return $name;
            });
            return [
                'data' => $collection,
                'pagination' => [
                    'total' => $this->total(),
                    'count' => $this->count(),
                    'per_page' => $this->perPage(),
                    'current_page' => $this->currentPage(),
                    'total_pages' => $this->lastPage()
                ],
            ];
        }


    }
}
